package cn.bdqn.controller;

import cn.bdqn.bean.result.Page;
import cn.bdqn.bean.result.Result;
import cn.bdqn.bean.result.ResultGenerator;
import cn.bdqn.domain.Druginfo;
import cn.bdqn.service.DruginfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/druginfo")
public class DruginfoController {

    @Autowired
    private DruginfoService druginfoService;

    //得到所有的药品列表
    @GetMapping("/queryAll")
    public Result queryAll(){
        List<Druginfo> druginfoList=druginfoService.queryAll();
        if (druginfoList != null){
            return ResultGenerator.genSuccessResult(druginfoList);
        }else {
            return ResultGenerator.genFailResult("未查到结果");
        }
    }

    //分页  得到所有的药品列表
    @GetMapping("/queryPage")
    public Result queryPage(){
        Page<Druginfo> druginfoList=druginfoService.queryPage(1);
        if (druginfoList != null){
            return ResultGenerator.genSuccessResult(druginfoList);
        }else {
            return ResultGenerator.genFailResult("未查到结果");
        }
    }

    //得到某药品的详细信息
    @GetMapping("/queryById")
    public Result queryById(Integer id){
        Druginfo druginfo=druginfoService.queryById(id);
        if (druginfo != null){
            return ResultGenerator.genSuccessResult(druginfo);
        }else {
            return ResultGenerator.genFailResult("未查到结果");
        }
    }

    //根据药品编号得到药品信息
    @GetMapping("/queryByNo")
    public Result queryByNo(Integer drugNo){
        Druginfo druginfo=druginfoService.queryByNo(drugNo);
        if (druginfo != null){
            return ResultGenerator.genSuccessResult(druginfo);
        }else {
            return ResultGenerator.genFailResult("未查到结果");
        }
    }

    //新增一条药品信息
    @PostMapping("/addSelective")
    public Result addSelective(Druginfo druginfo){
        try{
            druginfoService.addSelective(druginfo);
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("发生了错误");
        }
        return ResultGenerator.genSuccessResult();
    }

    //修改某条药品的信息
    @PostMapping("/changeBySelective")
    public Result changeByPrimaryKeySelective(Druginfo druginfo) {
        try {
            druginfoService.changeByPrimaryKeySelective(druginfo);
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("发生了错误");
        }
        return ResultGenerator.genSuccessResult();
    }

    //删除某条药品信息
    @PostMapping("/removeById")
    public Result removeById(Integer id){
        try{
            druginfoService.removeById(id);
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("发生了错误");
        }
        return ResultGenerator.genSuccessResult();
    }
}
